package com.taptech.doufu.services;

import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpUtil;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.LinkedList;
import java.util.List;

public class SweepFlowerService {

	public static final String BASE_URL = "http://" + Constant.HOST_NAME
			+ "/index.php/";

	public final static int SEND_FLOWER = 3000;
	public final static int HANDLE_TYPE_LOAD_FLOWER_RANK = 3001;
	public final static int HANDLE_TYPE_LOAD_SWEEP_REPLY = 3002;
	public final static int HANDLE_TYPE_COMMIT_SWEEP_REPLY = 3003;
	public final static int HANDLE_TYPE_COLLECT_SWEEP = 3004;
	public final static int HANDLE_TYPE_REMOVE_COLLECT_SWEEP = 3005;
	public final static int HANDLE_TYPE_HAS_COLLECT_SWEEP = 3006;
	public final static int HANDLE_TYPE_SWEEP_FLOWER = 3007;
	private static SweepFlowerService instance = new SweepFlowerService();

	private SweepFlowerService() {

	}

	public static SweepFlowerService getInstance() {
		return instance;
	}

	/**
	 * 献花
	 */
	public void sendFlower(String review_id, String num, HttpResponseListener listener) {
		HttpRequestObject request = new HttpRequestObject();
		request.setHandleType(SEND_FLOWER);
		List<NameValuePair> list = new LinkedList<NameValuePair>();
		NameValuePair value;
		value = new BasicNameValuePair("review_id", review_id);
		list.add(value);
		value = new BasicNameValuePair("num", num);
		list.add(value);
		request.setData(list);
		request.setUrl(BASE_URL + "novel_review/add_flower");
		request.setListener(listener);
		HttpUtil.sendPostRequest(request);
	}

	/**
	 * 献花的用户
	 */
	public void loadFlowerRank(String review_id, int pageIndex, int size, String last, HttpResponseListener listener) {
		HttpRequestObject request = new HttpRequestObject();
		request.setHandleType(HANDLE_TYPE_LOAD_FLOWER_RANK);
		request.setUrl(Constant.BASE_URL + "novel_review/flower_users?review_id="
				+ review_id + "&last=" + last + "&size=" + size);
		request.setListener(listener);
		HttpUtil.sendGetRequest(request);
	}

	/**
	 * 发表文评评论
	 */
	public void commitSweepReply(String sweep_id, String comment_content, HttpResponseListener listener) {
		HttpRequestObject request = new HttpRequestObject();
		request.setHandleType(HANDLE_TYPE_COMMIT_SWEEP_REPLY);
		List<NameValuePair> list = new LinkedList<NameValuePair>();
		NameValuePair value;
		value = new BasicNameValuePair("target_type", 29 + "");
		list.add(value);
		value = new BasicNameValuePair("target_id", sweep_id);
		list.add(value);
		value = new BasicNameValuePair("comment_content", comment_content);
		list.add(value);
		request.setData(list);
		request.setUrl(BASE_URL + "comment/post2/");
		request.setListener(listener);
		HttpUtil.sendPostRequest(request);
	}

	/**
	 * 文评评论列表
	 */
	public void loadSweepReplyList(String sweep_id, int pageIndex, String last, HttpResponseListener listener) {
		HttpRequestObject request = new HttpRequestObject();
		request.setHandleType(HANDLE_TYPE_LOAD_SWEEP_REPLY);
		request.setUrl(Constant.BASE_URL + "comment/index2?target_type=29&target_id="
				+ sweep_id + "&last=" + last + "&size=" + Constant.PAGE_SIZE_20);
		request.setListener(listener);
		HttpUtil.sendGetRequest(request);
	}

	/**
	 * 收藏文评
	 */
	public void loadSweepCollect(String sweep_id, HttpResponseListener listener) {
		HttpRequestObject request = new HttpRequestObject();
		request.setHandleType(HANDLE_TYPE_COLLECT_SWEEP);
		List<NameValuePair> list = new LinkedList<NameValuePair>();
		NameValuePair value;
		value = new BasicNameValuePair("id", sweep_id);
		list.add(value);
		value = new BasicNameValuePair("object_type", Constant.OBJECT_TYPE_SWEEP + "");
		list.add(value);
		request.setData(list);
		request.setUrl(BASE_URL + "favorite/add");
		request.setListener(listener);
		HttpUtil.sendPostRequest(request);
	}

	/**
	 * 取消收藏文评
	 */
	public void loadSweepRemoveCollect(String sweep_id, HttpResponseListener listener) {
		HttpRequestObject request = new HttpRequestObject();
		request.setHandleType(HANDLE_TYPE_REMOVE_COLLECT_SWEEP);
		List<NameValuePair> list = new LinkedList<NameValuePair>();
		NameValuePair value;
		value = new BasicNameValuePair("id", sweep_id);
		list.add(value);
		value = new BasicNameValuePair("object_type", Constant.OBJECT_TYPE_SWEEP + "");
		list.add(value);
		request.setData(list);
		request.setUrl(BASE_URL + "/favorite/remove");
		request.setListener(listener);
		HttpUtil.sendPostRequest(request);
	}

	/**
	 * 文评送花详情接口
	 */
	public void loadSweepFlowerDes(String review_id, HttpResponseListener listener) {
		HttpRequestObject request = new HttpRequestObject();
		request.setHandleType(HANDLE_TYPE_SWEEP_FLOWER);
		request.setUrl(Constant.BASE_URL + "novel_review/one?review_id=" + review_id);
		request.setListener(listener);
		HttpUtil.sendGetRequest(request);
	}
}
